package com.ruoyi.system.service.impl.video;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.user.VsDramasUserWalletLog;
import com.ruoyi.system.domain.video.VsDramasVideoEpisodes;
import com.ruoyi.system.mapper.user.VsDramasUserWalletLogMapper;
import com.ruoyi.system.mapper.user.VsUserMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoEpisodesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.video.VsDramasVideoOrderMapper;
import com.ruoyi.system.domain.video.VsDramasVideoOrder;
import com.ruoyi.system.service.video.IVsDramasVideoOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 视频订单Service业务层处理
 * 
 * @author zzx
 * @date 2024-04-30
 */
@Service
public class VsDramasVideoOrderServiceImpl implements IVsDramasVideoOrderService 
{
    @Autowired
    private VsDramasVideoOrderMapper vsDramasVideoOrderMapper;

    @Autowired
    private VsDramasVideoEpisodesMapper vsDramasVideoEpisodesMapper;

    @Autowired
    private VsDramasUserWalletLogMapper vsDramasUserWalletLogMapper;

    @Autowired
    private VsUserMapper vsUserMapper;

    /**
     * 查询视频订单
     * 
     * @param id 视频订单主键
     * @return 视频订单
     */
    @Override
    public VsDramasVideoOrder selectVsDramasVideoOrderById(Long id)
    {
        return vsDramasVideoOrderMapper.selectVsDramasVideoOrderById(id);
    }

    /**
     * 查询视频订单列表
     * 
     * @param vsDramasVideoOrder 视频订单
     * @return 视频订单
     */
    @Override
    public List<VsDramasVideoOrder> selectVsDramasVideoOrderList(VsDramasVideoOrder vsDramasVideoOrder)
    {
        return vsDramasVideoOrderMapper.selectVsDramasVideoOrderList(vsDramasVideoOrder);
    }

    /**
     * 新增视频订单
     * 
     * @param vsDramasVideoOrder 视频订单
     * @return 正常返回的是新增订单的id，这里判断返回-1代表余额不足，-2代表是免费的不需要再下订单，-9代表业务已经走通可以给前端返回视频链接
     */
    @Transactional
    @Override
    public int insertVsDramasVideoOrder(VsDramasVideoOrder vsDramasVideoOrder)
    {
        VsDramasVideoEpisodes episodes = vsDramasVideoEpisodesMapper.selectVsDramasVideoEpisodesById(Math.toIntExact(vsDramasVideoOrder.getEpisodeId()));
        if (episodes.getPrice() == 0) {
            return -2;
        }
        VsUser user = SecurityUtils.getLoginUser().getVsUser();
        VsUser vsUser1 = vsUserMapper.selectVsUserById(user.getId());
        // 积分不够返回-1
        if (vsUser1.getUsable() < episodes.getPrice()) {
            return -1;
        }
        // 如果这集视频不是免费的就查vip价格和vip信息

        boolean isVip = vsUser1.getVipExpiretime() != null && vsUser1.getVipExpiretime() >= System.currentTimeMillis();
        if (episodes.getVprice() == 0) {
            // 先判断用户是否为vip
            // 如果是vip判断vip价格是否免费，如果免费则直接返回视频链接，否则走购买视频流程
            if (isVip) {
                return -9;
            } else {
                vsDramasVideoOrder.setTotalFee(Long.valueOf(episodes.getPrice()));
            }
        } else {
            vsDramasVideoOrder.setTotalFee(Long.valueOf(episodes.getVprice()));
        }
        if (vsUser1.getUsable() < vsDramasVideoOrder.getTotalFee()) {
            return -1;
        }
        // 如果余额够的话生成用户钱包日志，扣除对应金额并生成订单信息
        vsDramasVideoOrder.setTotalFee(Long.valueOf(episodes.getVprice()));
        vsDramasVideoOrder.setPlatform("H5");
        vsDramasVideoOrder.setCreatetime(System.currentTimeMillis());
        vsDramasVideoOrder.setUpdatetime(System.currentTimeMillis());
        vsDramasVideoOrder.setOrderSn(System.currentTimeMillis() + IdUtils.fastSimpleUUID());
        vsDramasVideoOrder.setUserId(vsUser1.getId());
        int i = vsDramasVideoOrderMapper.insertVsDramasVideoOrder(vsDramasVideoOrder);

        VsDramasUserWalletLog userWalletLog = new VsDramasUserWalletLog();
        userWalletLog.setUserId(vsUser1.getId());
        String s = "-" + vsDramasVideoOrder.getTotalFee();
        BigDecimal bigDecimal = new BigDecimal(s);
        userWalletLog.setWallet(bigDecimal);
        userWalletLog.setWalletType("usable");
        userWalletLog.setType("used_video");
        userWalletLog.setBefore(BigDecimal.valueOf(vsUser1.getUsable()));
        BigDecimal decimal = BigDecimal.valueOf(vsUser1.getUsable()).add(bigDecimal);
        userWalletLog.setAfter(decimal);
        userWalletLog.setItemId(String.valueOf(i));
        if (episodes.getLangId() == 65) {
            userWalletLog.setMemo("付费看剧");
        } else {
            userWalletLog.setMemo("Pay for watching dramas");
        }
        userWalletLog.setOperType(vsUser1.getMode());
        userWalletLog.setOperId(vsUser1.getId());
        userWalletLog.setCreatetime(System.currentTimeMillis());
        userWalletLog.setUpdatetime(System.currentTimeMillis());
        vsDramasUserWalletLogMapper.insertVsDramasUserWalletLog(userWalletLog);
        VsUser vsUser = new VsUser();
        vsUser.setId(vsUser1.getId());
        vsUser.setUsable(decimal.longValue());
        vsUserMapper.updateVsUser(vsUser);
        return -9;
    }

    /**
     * 修改视频订单
     * 
     * @param vsDramasVideoOrder 视频订单
     * @return 结果
     */
    @Override
    public int updateVsDramasVideoOrder(VsDramasVideoOrder vsDramasVideoOrder)
    {
        vsDramasVideoOrder.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoOrderMapper.updateVsDramasVideoOrder(vsDramasVideoOrder);
    }

    /**
     * 批量删除视频订单
     * 
     * @param ids 需要删除的视频订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoOrderByIds(Long[] ids)
    {
        return vsDramasVideoOrderMapper.deleteVsDramasVideoOrderByIds(ids);
    }

    /**
     * 删除视频订单信息
     * 
     * @param id 视频订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoOrderById(Long id)
    {
        return vsDramasVideoOrderMapper.deleteVsDramasVideoOrderById(id);
    }
}
